import { toast } from "@/utils";
import { getToken } from "@/utils/auth";
import Taro from "@tarojs/taro";

export default (img: string, formData?) => {
  Taro.showLoading({ title: "加载中" });
  return new Promise((resolve, reject) => {
    return Taro.uploadFile({
      // url: process.env.TARO_APP_API + "/upload/uploadFile?bucket=forum", //仅为示例，非真实的接口地址
      url: "http://127.0.0.1:9999/upload/uploadFile?bucket=forum", //仅为示例，非真实的接口地址
      filePath: img,
      name: "file",
      formData,
      header: {
        Authorization: getToken() ?? null,
      },
      success(res) {
        const { code, msg, data } = JSON.parse(res.data);
        if (code == 20001) {
          reject(msg);
          toast(msg, "error");
        } else {
          toast(msg);
          resolve(data.absPath[0]);
        }
      },
      complete: () => Taro.hideLoading(),
    });
  });
};
